//
//  NSObject+XBAdd.m
//  XBCategoriesDemo
//
//  Created by kevin007 on 2021/4/12.
//

#import "NSObject+XBAdd.h"
#import <objc/runtime.h>

@implementation NSObject (XBAdd)
/// 延迟调用方法，延时时间内，如果频发触发调用时自动取消前一次方法调用
/// @param selector SEL
/// @param object any object
/// @param second 延迟时间 单位秒
- (void)previousPerformSelector:(SEL)selector
                     withObject:(id)object
                     afterDelay:(NSTimeInterval)second {
    //取消上次请求
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:object];
    //发起新的延迟请求
    [self performSelector:selector withObject:object afterDelay:second];
}

- (void)setAssociateValue:(id)value withKey:(void *)key {
    objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setAssociateWeakValue:(id)value withKey:(void *)key {
    objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_ASSIGN);
}

- (void)removeAssociatedValues {
    objc_removeAssociatedObjects(self);
}

- (id)getAssociatedValueForKey:(void *)key {
    return objc_getAssociatedObject(self, key);
}

@end
